//
//  datePickerView.m
//  YOYOLive
//
//  Created by 王根强 on 2016/10/26.
//  Copyright © 2016年 王根强. All rights reserved.
//

#import "YODatePickerView.h"

@interface YODatePickerView () {
    UIDatePicker *datePicker;
    UILabel *nameLb;
    UILabel *constellationLb;
    
    int age;
    NSString *xingzuoStr;
    NSString *dateStr;
}

@end

@implementation YODatePickerView
- (id) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initWithAllSubviews];
    }
    return self;
}

- (void)initWithAllSubviews {
    //遮罩层
    UIView *view_cover = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:view_cover];
    view_cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    //手势移除
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
    [view_cover addGestureRecognizer:gesture];
    
    //展示输入内容
    UIView *view_showInputView = [[UIView alloc] initWithFrame:CGRectMake(0, kGScreenHeight, kGScreenWidth, 242)];
    [view_cover addSubview:view_showInputView];
    view_showInputView.backgroundColor = RGB(245, 240, 236);
    
    UILabel *lab = [[UILabel alloc]init];
    [view_showInputView addSubview:lab];
    [lab setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont13 withText:@"滚动时间，系统自动计算年龄、星座" withTextColor:RGB(181, 181, 181) withTextAlignment:(NSTextAlignmentCenter)];
    lab.frame = CGRectMake(0, 10, view_showInputView.width, 25);

    UILabel *namelb = [[UILabel alloc]init];
    [view_showInputView addSubview:namelb];
    [namelb setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont17 withText:@"年龄:" withTextColor:RGB(65, 65, 65) withTextAlignment:(NSTextAlignmentLeft)];
    namelb.frame = CGRectMake(90*KWIDTH_6PLUS_SCALE, 10, 45*KWIDTH_6PLUS_SCALE, 20);
    namelb.top = lab.bottom + 5;
    
    nameLb = [[UILabel alloc]init];
    [view_showInputView addSubview:nameLb];
    [nameLb setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont17 withText:[NSString stringWithFormat:@"%@岁",@"100"] withTextColor:RGB(65, 65, 65) withTextAlignment:(NSTextAlignmentLeft)];
    nameLb.frame = CGRectMake(0, 0, 45*KWIDTH_6PLUS_SCALE, 20);
    nameLb.centerY = namelb.centerY;
    nameLb.left = namelb.right;

    UILabel *constellationlb = [[UILabel alloc]init];
    [view_showInputView addSubview:constellationlb];
    [constellationlb setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont17 withText:@"星座:" withTextColor:RGB(65, 65, 65) withTextAlignment:(NSTextAlignmentLeft)];
    constellationlb.frame = CGRectMake(0, 0, 45*KWIDTH_6PLUS_SCALE, 20);
    constellationlb.centerY = namelb.centerY;
    constellationlb.left = nameLb.right + 30*KWIDTH_6PLUS_SCALE;
    
    constellationLb = [[UILabel alloc]init];
    [view_showInputView addSubview:constellationLb];
    [constellationLb setLableStyleWithBackgroundColor:[UIColor clearColor] withFont:kFont17 withText:@"白羊座" withTextColor:RGB(65, 65, 65) withTextAlignment:(NSTextAlignmentLeft)];
    constellationLb.frame = CGRectMake(0, 0, 60*KWIDTH_6PLUS_SCALE, 20);
    constellationLb.centerY = namelb.centerY;
    constellationLb.left = constellationlb.right;
    
    datePicker = [[UIDatePicker alloc] init];
    [datePicker setDatePickerMode:UIDatePickerModeDate];//选择date的显示模式
    datePicker.frame = CGRectMake(0, 0, kGScreenWidth, 162);
    datePicker.top = constellationLb.bottom + 10;
    datePicker.backgroundColor = RGB(245, 240, 236);
    //设置最小日期值
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *minDate = [dateFormatter dateFromString:@"1900-01-01"];
    NSDate *maxDate = [NSDate date];
    [datePicker setMaximumDate:maxDate];
    [datePicker setMinimumDate:minDate];
    if ([@"" isEqualToString:@""]) {
        [datePicker setDate:[dateFormatter dateFromString:@"1996-01-01"] animated:YES];
        nameLb.text = @"20岁";
        constellationLb.text = @"摩羯座";
    }else {
        [datePicker setDate:[dateFormatter dateFromString:@"1996-01-01"] animated:YES];
    }
    //设置时区
    NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [datePicker setLocale:locale];
    //当值发生改变的时候调用的方法
    [datePicker addTarget:self action:@selector(datePickerDateChanged:) forControlEvents:UIControlEventValueChanged];
    [view_showInputView addSubview:datePicker];
    
    [UIView animateWithDuration:0.3 animations:^{
            view_showInputView.frame = CGRectMake(0, kGScreenHeight - 242, kGScreenWidth, 242);
    }];
}
     
- (NSString *)getXingzuo:(NSDate *)inDate {
    //计算星座
    NSString *retStr=@"";
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM"];
    int monthInt=0;
    NSString *theMonth = [dateFormat stringFromDate:inDate];
    if([[theMonth substringToIndex:0] isEqualToString:@"0"]){
        monthInt = [[theMonth substringFromIndex:1] intValue];
    }else{
        monthInt = [theMonth intValue];
    }
    
    [dateFormat setDateFormat:@"dd"];
    int dayInt=0;
    NSString *theDay = [dateFormat stringFromDate:inDate];
    if([[theDay substringToIndex:0] isEqualToString:@"0"]){
        dayInt = [[theDay substringFromIndex:1] intValue];
    }else{
        dayInt = [theDay intValue];
    }
    /*
     摩羯座 12月22日------1月19日
     水瓶座 1月20日-------2月18日
     双鱼座 2月19日-------3月20日
     白羊座 3月21日-------4月19日
     金牛座 4月20日-------5月20日
     双子座 5月21日-------6月21日
     巨蟹座 6月22日-------7月22日
     狮子座 7月23日-------8月22日
     处女座 8月23日-------9月22日
     天秤座 9月23日------10月23日
     天蝎座 10月24日-----11月21日
     射手座 11月22日-----12月21日
     */
    if (monthInt == 1) {
        if (dayInt > 0 && dayInt < 20) {
            retStr=@"摩羯座";
        }
        if (dayInt > 21 &&dayInt < 31) {
            retStr=@"水瓶座";
        }
        return retStr;
    }
    if (monthInt == 2) {
        if (dayInt > 0 && dayInt < 19) {
            retStr=@"水瓶座";
        }
        if (dayInt > 20 && dayInt < 30) {
            retStr=@"双鱼座";
        }
        return retStr;
    }
    if (monthInt == 3) {
        if (dayInt > 0 && dayInt < 21) {
            retStr=@"双鱼座";
        }
        if (dayInt > 20 && dayInt < 32) {
            retStr=@"白羊座";
        }
        return retStr;
    }
    if (monthInt == 4) {
        if (dayInt > 0 && dayInt < 20) {
            retStr=@"白羊座";
        }
        if (dayInt > 19 && dayInt < 31) {
            retStr=@"金牛座";
        }
        return retStr;
    }
    if (monthInt == 5) {
        if (dayInt > 0 && dayInt < 21) {
            retStr=@"金牛座";
        }
        if (dayInt > 22 && dayInt < 32) {
            retStr=@"双子座";
        }
        return retStr;
    }
    if (monthInt == 6) {
        if (dayInt > 0 && dayInt < 22) {
            retStr=@"双子座";
        }
        if (dayInt > 21 && dayInt < 31) {
            retStr=@"巨蟹座";
        }
        return retStr;
    }
    if (monthInt == 7) {
        if (dayInt > 0 && dayInt < 23) {
            retStr=@"巨蟹座";
        }
        if (dayInt > 22 && dayInt < 32) {
            retStr=@"狮子座";
        }
        return retStr;
    }
    if (monthInt == 8) {
        if (dayInt > 0 && dayInt < 23) {
            retStr=@"狮子座";
        }
        if (dayInt > 22 && dayInt < 32) {
            retStr=@"处女座";
        }
        return retStr;
    }
    if (monthInt == 9) {
        if (dayInt >= 1 && dayInt < 23) {
            retStr=@"处女座";
        }
        if (dayInt > 22 && dayInt < 31) {
            retStr=@"天秤座";
        }
        return retStr;
    }
    if (monthInt == 10) {
        if (dayInt > 0 && dayInt < 24) {
            retStr=@"天秤座";
        }
        if (dayInt > 23 && dayInt < 32) {
            retStr=@"天蝎座";
        }
        return retStr;
    }
    if (monthInt == 11) {
        if (dayInt > 0 && dayInt < 22) {
            retStr=@"天蝎座";
        }
        if (dayInt > 21 && dayInt < 31) {
            retStr=@"摩羯座";
        }
        return retStr;
    }
    if (monthInt == 12) {
        if (dayInt >= 1 && dayInt < 22) {
            retStr=@"水瓶座";
        }
        if (dayInt > 21 && dayInt < 32) {
            retStr=@"摩羯座";
        }
        return retStr;
    }
    return  retStr;
}

//date改变的代理事件
- (void) datePickerDateChanged:(UIDatePicker *)paramDatePicker {
    if ([paramDatePicker isEqual:datePicker]) {
        NSDate *selected = [datePicker date];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy-MM-dd"];
        NSTimeInterval dateDiff = [selected timeIntervalSinceNow];
        age=-1 * trunc(dateDiff/(60*60*24))/365;
        xingzuoStr = [self getXingzuo:selected];
        dateStr = [dateFormat stringFromDate:[datePicker date]];
        nameLb.text = [NSString stringWithFormat:@"%d岁",age];
        constellationLb.text = xingzuoStr;
    }
}

- (void)dismissView {
    if (self.removeView) {
        NSDate *selected = [datePicker date];
        xingzuoStr = [self getXingzuo:selected];
        DebugLog(@"%d-%@",age,dateStr);
        if (dateStr == nil) {
            self.removeView([NSString stringWithFormat:@"%d,%@,%@", 20, @"1996-01-01", @"摩羯座"]);
        }else {
            self.removeView([NSString stringWithFormat:@"%d,%@,%@", age, dateStr, xingzuoStr]);
        }
    }
}

@end
